package main

import (
	"common/log"
	"common/process"
	"errors"
	"euplink"
	"fmt"
	"github.com/spf13/cobra"
	"os"
)

var downloadCmd = &cobra.Command{
	Use:   "download",
	Short: "下载",
	Long:  "euplink download (remote_src) (local_dsc)",
	RunE:  cmdDownload,
}

func cmdDownload(cmd *cobra.Command, args []string) error {
	ctx, _ := process.Ctx(cmd)
	if len(args) != 2 {
		return errors.New("参数错误")
	}
	zapLog := log.NewZapLog(&runConfig.Log)
	prj, err := euplink.NewProject(ctx, runConfig.Project, zapLog)
	if err != nil {
		return fmt.Errorf("project init err: %s", err)
	}
	outFile, err := os.OpenFile(args[1], os.O_WRONLY|os.O_CREATE, os.ModePerm)
	if err != nil {
		fmt.Println("download:create file: ", args[1], " err: ", err)
	}
	defer outFile.Close()
	return prj.DownloadObject(ctx, runConfig.Project.DefaultBucket, args[0], outFile)
}
